package com.chinanet.controller.faultanticipate;

import com.chinanet.entry.faultanticipate.Detail;
import com.chinanet.entry.faultanticipate.Record;
import com.chinanet.querydsl.predicate.PredicateService;
import com.chinanet.querydsl.predicate.faultanticipate.DetailPredicatesBuilder;
import com.chinanet.repository.faultanticipate.DetailRepository;
import com.chinanet.service.faultanticipate.IDetailService;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resources;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RepositoryRestController
@Slf4j
@RestController
@RequestMapping(value = "/details/operation")
public class DetailController {

    @Resource
    private DetailRepository detailRepository;

    @Autowired
    private EntityLinks links;

    @Autowired
    private IDetailService detailService;

    @Autowired
    private PredicateService predicateService;

    @RequestMapping(value = "name/{name}", method = RequestMethod.GET)
    public Detail name(@PathVariable String name) throws UnsupportedEncodingException {
        String newName = URLDecoder.decode(name, "UTF-8");
        System.out.println(newName);
        Detail detail = detailRepository.findByName(newName);
        if (detail == null) {
            throw new NullPointerException();
        } else {
            return detail;
        }
    }

    @RequestMapping(value = "/copy", method = RequestMethod.POST)
    public long copy(@RequestBody Detail detail) {
        return detailService.copy(detail);
    }

    @RequestMapping(value = "/model/{detailId}", method = RequestMethod.POST)
    public Record getModelByDetail(@PathVariable long detailId) {
        return detailService.getRecordByDetail(detailId);
    }

    @RequestMapping(value = "team/{teamId}", method = RequestMethod.GET)
    public
    PagedResources getAllByTeam(@PathVariable long teamId, Pageable pageable, PagedResourcesAssembler assembler, PersistentEntityResourceAssembler persistentEntityResourceAssembler) {
        Page<Detail> details = detailRepository.getAllByTeam(teamId, pageable);
        return assembler.toResource(details, persistentEntityResourceAssembler);
    }

    // 搜索操作
    @RequestMapping(method = RequestMethod.GET, value = "/details")
    public @ResponseBody
    Resources<Detail> search(@RequestParam(value = "search", required = true, defaultValue = "null") String search, PersistentEntityResourceAssembler persistentEntityResourceAssembler) {
        DetailPredicatesBuilder builder = new DetailPredicatesBuilder();
        if (search.equals("null")) {
            Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
            Matcher matcher = pattern.matcher(search + ",");
            while (matcher.find()) {
                builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
                log.error("报错");
            }
        }
        BooleanExpression exp = builder.build();
        return new Resources<Detail>(detailRepository.findAll(exp));
    }

    public @ResponseBody
    PagedResources join(@RequestParam MultiValueMap<String, String> parameters, @PathVariable long roleId, Pageable pageable, PagedResourcesAssembler assembler, PersistentEntityResourceAssembler persistentEntityResourceAssembler) {
       System.out.println(parameters);
        Predicate predicate = predicateService.getPredicateFromParameters(parameters, Detail.class);
        return assembler.toResource(detailRepository.findAll(predicate, pageable), persistentEntityResourceAssembler);
    }
    @GetMapping("role/{roleId}")
    public PagedResources getAllByRole(@PathVariable long roleId, Pageable pageable, PagedResourcesAssembler assembler, PersistentEntityResourceAssembler persistentEntityResourceAssembler){
        return assembler.toResource(detailService.getAllByRole(roleId,pageable), persistentEntityResourceAssembler);
    }
}
